package org.kreedx.demo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import org.kreedx.view.ui.FontView;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.nio.charset.Charset;

import io.reactivex.functions.Consumer;
import okio.BufferedSource;
import okio.Okio;
import okio.Source;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getName();
    private MyHander hander;
    private String text;
    private FontView fontView;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fontView = findViewById(R.id.font_view);
        fontView.setText("hello font view");


        hander = new MyHander(new WeakReference<>(this));


        /*if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(new Consumer<Permission>() {
                        @Override
                        public void accept(Permission permission) throws Exception {
                            if (permission.granted) {
                                // 用户已经同意该权限
                                Log.d(TAG, permission.name + " is granted.");
                                new Thread(new ReadRunable()).start();
                            } else if (permission.shouldShowRequestPermissionRationale) {
                                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                                Log.d(TAG, permission.name + " is denied. More info should be provided.");
                            } else {
                                // 用户拒绝了该权限，而且选中『不再询问』
                                Log.d(TAG, permission.name + " is denied.");
                            }
                        }
                    });
        }else {
            new Thread(new ReadRunable()).start();
        }*/

        new Thread(new ReadRunable()).start();




        new Thread(new Runnable() {
            @Override
            public void run() {
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
                String result = restTemplate.getForObject("https://www.easy-mock.com/mock/59791048a1d30433d83eb728/example/mock",String.class);
                Log.e("res",result);
            }
        }).start();
    }

    public static class MyHander extends Handler{
        WeakReference<MainActivity> mainActivityWeakReference;

        private MyHander(WeakReference<MainActivity> mainActivityWeakReference) {
            this.mainActivityWeakReference = mainActivityWeakReference;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what == 10086){
                Bundle bundle = msg.getData();
                String text = bundle.getString("data");
                mainActivityWeakReference.get().text = text;
                mainActivityWeakReference.get().fontView.setText(text);
            }
        }
    }

    public class ReadRunable implements Runnable{

        @Override
        public void run() {
            Source source = null;
            BufferedSource bSource = null;
            String read = null;
            try {
                InputStream inputStream = getResources().getAssets().open("all.txt");
                source = Okio.source(inputStream);
                //通过source拿到 bufferedSource
                bSource = Okio.buffer(source);
                read = bSource.readString(Charset.forName("utf-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Bundle bundle = new Bundle();
            bundle.putString("data",read);
            Message message = hander.obtainMessage();
            message.setData(bundle);
            message.what = 10086;
            hander.dispatchMessage(message);
        }
    }
}
